/**
 * Project Name:YCApiServer-Api
 * File Name:UserCenterService.java
 * Package Name:com.yc.music.service.service103
 * Date:2016年5月10日下午1:25:36
 * Copyright (c) 2016 
 *
 */
package com.yc.music.service.service21;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.yc.music.common.context.YcContext;
import com.yc.music.common.util.RoomUtil;
import com.yc.music.context.YtConstant;
import com.yc.music.mapper.cache.MusicActivityCacheMapper;
import com.yc.music.mapper.dao.MusicActivityFlowMapper;
import com.yc.music.model.activity.MusicActivityFlow;
import com.yc.music.model.activity.MusicActivityVo;

/**
 * 
 * ClassName: MusicActivityService <br/>
 * Function: 活动. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年8月15日 下午2:32:17 <br/>
 *
 * @author qinhaiguan
 * @version
 */
@Service
public class MusicActivityService {

	@Value("${Pic_QiLiu}")
	private String pIC_QiLiu;
	
	@Value("${Audio_QiLiu}")
	private String Audio_QiLiu;
	
	@Value("${audio_buckname}")
	private String audio_buckname;
	
	@Value("${pic_buckname}")
	private String pic_buckname;
	
	@Autowired
	private MusicActivityCacheMapper musicActivityCacheMapper;
	
	@Autowired
	private MusicActivityFlowMapper musicActivityFlowMapper;

	
	public Map<String,Object> getMusicActivityList(Integer page,Map<String,Object> result) throws Exception {
		int rows=10;
		page=page==0?page=1:page;
		int start=(page-1)*rows;
		List<MusicActivityVo> list = musicActivityCacheMapper.getMusicActivityPage(start, rows);
		for (MusicActivityVo musicActivityVo : list) {
			musicActivityVo.setPic(RoomUtil.jointDomain(musicActivityVo.getPic(),pIC_QiLiu));
		}
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, list);
		return result;
	}
	
	
	
	public Map<String,Object> getMusicActivityDetail(Integer aid,long uid,Map<String,Object> result) throws Exception {
		int isJoin=0;
		//查询用户是否参加过该活动
		MusicActivityFlow flow=musicActivityFlowMapper.getActivityFlowById(aid,uid);
		if (null!=flow) {
			synchronized (MusicActivityService.class) {
				isJoin=1;
			}
		}
		MusicActivityVo  musicActivityVo  = musicActivityCacheMapper.getMusicActivityById(aid);
		if(musicActivityVo==null) {
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, YtConstant.ACTIVITY_IS_NOT_EXIST);
			result.put(YcContext.DATA, YcContext.EMPTY_STRING);
			return result;
		}
		
		//封面地址
		musicActivityVo.setPic(RoomUtil.jointDomain(musicActivityVo.getPic(),pIC_QiLiu));
		
		Map<String,Object> map=new ConcurrentHashMap<String,Object>();
		//是否参加过活动
		map.put("isJoin", isJoin);
		
		//活动详情
		map.put("activityDetail", musicActivityVo);
		
		//作品数量
		musicActivityVo.setWorknum(musicActivityFlowMapper.getWorkCount(aid));
		
		//参与人数
		musicActivityVo.setJoinnum(musicActivityFlowMapper.getUserCount(aid));
		
		result.put(YcContext.CODE, 200);
		result.put(YcContext.MESSAGE, YcContext.EMPTY_STRING);
		result.put(YcContext.DATA, map);
		//浏览量
		musicActivityCacheMapper.updateMusicActivityLooknum(aid);
		return result;
	}
	
}
